package com.yanqu.road.server.cmd.tradewar;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eTradeWarBattleType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.config.GoodsMgr;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarActivity;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarActivityMgr;
import com.yanqu.road.server.manager.tradewar.CrossTradeWarMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = Protocol.C_CROSS_TRADE_WAR_TO_ENEMY_BATTLE_RESP, desc = "跨服商战到对手区服战斗返回")
public class CrossTradeWarToEnemyBattleRespCmd extends CrossUnionCommand {
    private long myUserId;
    private long myServerId;
    private int activityId;
    private int fastTeamNum;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg reqMsg = CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg.parseFrom(packet.getBytes());
        myUserId = reqMsg.getMyUserId();
        myServerId = reqMsg.getMyServerId();
        activityId = reqMsg.getActivityId();
        fastTeamNum = reqMsg.getFastTeamNum();
        if(reqMsg.getRet() == 0) {
            int trackResult = 0;

            int groupId = Cross2UnionActivityGroupMgr.getGroupId(activityId, myUserId);
            CrossTradeWarActivity activity = CrossTradeWarMgr.getActivity(activityId, groupId);

            if (reqMsg.getBattleType() == eTradeWarBattleType.Track.getValue()
                    && !CrossTradeWarMgr.isCrossTrackChange(reqMsg.getTrackChangeType())) {
                //增加追缉记录
                if (activity == null){
                    log.error("activityId = {} 的跨服商战活动的分组{}活动不存在",activityId,groupId);
                } else {
                    trackResult = activity.addTrackRecordByResult(reqMsg);
                }

                if (0 != trackResult) {//追缉人数满了
                    CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg.Builder tempMsg = reqMsg.toBuilder();
                    if(-1 == trackResult){
                        tempMsg.setRet(GameErrorCode.E_TRADE_WAR_NO_PREPARE);
                    }
                    tempMsg.setTrackChangeType(eTradeWarBattleType.Challenge.getValue());
                    //免除扣分
                    tempMsg.setEnemyLoseScore(0);
                    //免除通缉道具加分加成
                    int myAddScore = reqMsg.getMyAddScore();
                    GoodsInfo wantedGoods = GoodsMgr.getGoodsInfo(reqMsg.getWantedItemId());
                    if(null != wantedGoods){
                        int param2 = wantedGoods.getParamList().get(1).intValue();
                        if(0 != param2) {
                            myAddScore = myAddScore / param2;
                        }
                    }
                    tempMsg.setMyAddScore(myAddScore);
                    //重新算奖励，把荣誉币加成扣除
                    Property reward = PropertyHelper.parseStringToProperty(reqMsg.getBattleReward());
                    if(null != reward){
                        long tradeWarScore = reward.getCountByGoodsId(GameConfig.GAME_MONEY_CROSS_TRADE_WAR_SCORE).longValue();
                        long newTradeWarScore = tradeWarScore;
                        if(null != wantedGoods){
                            int param2 = wantedGoods.getParamList().get(1).intValue();
                            if(0 != param2) {
                                newTradeWarScore = newTradeWarScore / param2;
                            }
                        }
                        reward.setGood(GameConfig.GAME_MONEY_CROSS_TRADE_WAR_SCORE, BigInteger.valueOf(newTradeWarScore));
                        tempMsg.setBattleReward(PropertyHelper.parsePropertyToString(reward));
                    }
                    reqMsg = tempMsg.build();
                }
                int tempBattleType = reqMsg.getBattleType();
                if(CrossTradeWarMgr.isCrossTrackChange(reqMsg.getTrackChangeType())){
                    tempBattleType = reqMsg.getTrackChangeType();
                }
                //到对手服增加记录
                CrossTradeWarProto.CrossTradeWarNotifyEnemyAddRecordMsg.Builder notifyMsg = CrossTradeWarProto.CrossTradeWarNotifyEnemyAddRecordMsg.newBuilder();
                notifyMsg.setEnemyUserId(reqMsg.getEnemyUserId());
                notifyMsg.setBattleType(tempBattleType);
                notifyMsg.setRecordUid(reqMsg.getRecordUid());
                notifyMsg.setKillPatronsCount(reqMsg.getKillPatronsCount());
                notifyMsg.setEnemyLoseScore(reqMsg.getEnemyLoseScore());
                if(-1 == trackResult){//重复追缉
                    notifyMsg.setIsCancel(true);
                }
                //发到对手区服，通知加记录
                YanQuMessage pbNotify = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_NOTIFY_ENEMY_ADD_RECORD, notifyMsg);
                MessageHelper.sendPacket(serverId, reqMsg.getEnemyUserId(), pbNotify);
            }
            //增加商战公告
            if(-1 != trackResult && activity != null) {//重复追缉不会加公告
                activity.addNoticeByResult(reqMsg);
            }
        }

        //发回原区服，战斗结束
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_TO_ENEMY_BATTLE_RESP, reqMsg);
        MessageHelper.sendPacket(myServerId, myUserId, pbMsg);
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

    }

    @Override
    public void sendData() {

    }
}
